package co.edu.unal.cg.splines;

import java.util.ArrayList;

import org.la4j.linear.LinearSystem;
import org.la4j.matrix.Matrices;
import org.la4j.matrix.Matrix;
import org.la4j.matrix.dense.Basic2DMatrix;
import org.la4j.matrix.dense.DenseSafeMatrix;
import org.la4j.matrix.sparse.CRSMatrix;
import org.la4j.vector.Vector;
import org.la4j.vector.dense.BasicVector;

import processing.core.PApplet;
import processing.core.PVector;

public abstract class Spline {

	double stepSize;

	public Spline() {
		this(0.1);
	}

	public Spline(double stepSize) {
		this.stepSize = stepSize;
	}

	public void draw(PApplet canvas) {
		ArrayList<Matrix> c = getCoefficients();
		if (c != null) {
			for (int i = 0; i < c.size(); i++)
				drawSegment(c.get(i), canvas);
		}
	}

	private void drawSegment(Matrix ds, PApplet canvas) {
		double u = 0.0d;
		while (u + stepSize <= 1) {
			Vector v = new BasicVector(new double[] { Math.pow(u, 3),
					Math.pow(u, 2), Math.pow(u, 1), 1 });
			Vector p = ds.multiply(v);
			u += stepSize;
			v = new BasicVector(new double[] { Math.pow(u, 3), Math.pow(u, 2),
					Math.pow(u, 1), 1 });
			Vector p2 = ds.multiply(v);
			canvas.line((float) p.get(0), (float) p.get(1), (float) p.get(2),
					(float) p2.get(0), (float) p2.get(1), (float) p2.get(2));
		}
	}

	public abstract ArrayList<Matrix> getCoefficients();
}
